package cn.kgc.service.impl;

import cn.kgc.dao.AppMapper;
import cn.kgc.pojo.AppCategory;
import cn.kgc.pojo.AppInfo;
import cn.kgc.pojo.AppVersion;
import cn.kgc.pojo.DataDictionary;
import cn.kgc.service.AppSercive;
import cn.kgc.util.PageDto;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppSerciveImpl implements AppSercive {
    @Autowired
    private AppMapper appMapper;


    @Override
    public Integer totalRecodeQ(AppInfo appInfo)throws Exception  {
        int i = appMapper.totalRecode(appInfo.getSoftwareName(), appInfo.getFlatformId(), appInfo.getCategoryLevel1(), appInfo.getCategoryLevel2(), appInfo.getCategoryLevel3(), appInfo.getDevId(), appInfo.getStatus());
        return i;
    }

    @Override
    public PageDto queryAll(Integer pageNumber, AppInfo appInfo) throws Exception {
        //每页显示条数
        int pageSize = 4;
        //每页开始数据下标
        int beginIndex = (pageNumber - 1) * pageSize;
        //一共多少条数据
        int totalRecode = totalRecodeQ(appInfo);
        //计算一共有几页数据
        int totalPage = totalRecode % pageSize == 0 ? totalRecode / pageSize : totalRecode / pageSize + 1;
        PageDto pageDto = new PageDto();
        pageDto.setTotalPages(totalPage);
        pageDto.setPageSize(pageSize);
        pageDto.setTotalCount(totalRecode);
        pageDto.setPageNumber(pageNumber);
        //获取查询到的所有数据
        List<AppInfo> appInfoList = appMapper.queryAll(beginIndex, pageSize, appInfo.getSoftwareName(), appInfo.getFlatformId(), appInfo.getCategoryLevel1(), appInfo.getCategoryLevel2(), appInfo.getCategoryLevel3(), appInfo.getDevId(), appInfo.getStatus());
        pageDto.setPageData(appInfoList);
        return pageDto;
    }

    @Override
    public List<DataDictionary> listPlatform(@Param("typeName") String typeName) throws Exception {
        List<DataDictionary> dataDictionaryList = appMapper.listPlatform(typeName);
        return dataDictionaryList;
    }

    @Override
    public List<AppCategory> listLv1()throws Exception  {
        List<AppCategory> list = appMapper.listLv1();
        return list;
    }

    @Override
    public List<AppCategory> listLv2(Integer id)throws Exception  {
        List<AppCategory> list = appMapper.listLv2(id);
        return list;
    }


    @Override
    public List<AppInfo> queryById(Integer id) throws Exception {
        List<AppInfo> list=appMapper.queryById(id);
        return list;
    }

    @Override
    public List<AppVersion> queryVersionById(Integer id) throws Exception {
        List<AppVersion> list=appMapper.queryVersionById(id);
        return list;
    }

    @Override
    public Integer updateStatus(@Param(value = "id") Integer id, @Param(value = "status") Integer status) throws Exception {
        return appMapper.updateStatus(id,status);
    }

    @Override
    public List<AppVersion> appVersionL(Integer appId) throws Exception {
        List<AppVersion> list=appMapper.appVersionL(appId);
        return list;
    }

    @Override
    public int modifies(AppVersion appVersion) throws Exception {
        return appMapper.modifies(appVersion);
    }

    @Override
    public List<AppVersion> modifies1(Integer appId) throws Exception {
        return appMapper.modifies1(appId);
    }

    @Override
    public int updateVersionId(@Param("id") Integer id, @Param("versionId") Integer versionId) throws Exception {
        return appMapper.updateVersionId(id,versionId);
    }

    @Override
    public int deleteVI(Integer id) throws Exception {
        return appMapper.deleteVI(id);
    }
}